package com.kaifamiao.N0802;

public class NumberFormatter1 {
    /**
     * 转 16 进制
     *
     * @param num
     * @return
     */
    public String toHex(int num) {
        int[] nums = new int[100];
        int index = 0;
        for (int i = num; i > 0; i /= 16, index++) {
            nums[index] = i % 16;
        }
        for (int i = index - 1; i >= 0; i--) {
            if (nums[i] == 10) {
                System.out.print("A");
            } else if (nums[i] == 11) {
                System.out.print("B");
            } else if (nums[i] == 12) {
                System.out.print("C");
            } else if (nums[i] == 13) {
                System.out.print("D");
            } else if (nums[i] == 14) {
                System.out.print("E");
            } else if (nums[i] == 15) {
                System.out.print("F");
            } else {
                System.out.print(nums[i]);
            }

        }return String.valueOf(nums[index]);
    }
        /**
         * 转 8 进制
         * @param num
         * @return
         */
        public String toOctal ( int num){
            String sum = "";
            for (int i = num; i > 0; i /=8) {
                sum = i % 8 + sum;
            }
               return sum;
        }

        /**
         * 转 2 进制
         * @param num
         * @return
         */
        public String toBinary ( int num){
            String sum = "";
            for (int i = num; i > 0; i /= 2) {
                sum = i % 2 + sum;
            }
            return sum;
        }

        /**
         * 2 进制转 10 进制
         * @param str
         * @return
         */
        public int binaryToInt (String str){
            String sum = "";

            return 0;
        }

    }